﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration.Install;
using System.ComponentModel;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;

namespace ExtendADSchema
{    
    public static class ExtendSchema
    {
        public static void Extend()
        {
            ActiveDirectorySchema schema = Forest.GetCurrentForest().Schema;
            

            //DirectoryEntry schemaEntry = schema.GetDirectoryEntry();

            //
            //DirectoryEntry attribute = schemaEntry.Children.Add("cn=Novakov-Group-Mapped-Disk", "attributeSchema");
            //attribute.Properties["attributeID"].Add("1.2.840.113556.1.8000.2554.45983.5507.44833.19347.44343.13141437.2987128.1");
            //attribute.Properties["attributeSyntax"].Add("2.5.5.12");
            //attribute.Properties["isSingleValued"].Add(true);
            //attribute.Properties["showInAdvancedViewOnly"].Add(true);
            //attribute.Properties["adminDisplayName"].Add("Novakov-Group-Mapped-Disk");
            //attribute.Properties["oMObjectClass"].Add("KwwCh3McAIVK");
            //attribute.Properties["adminDescription"].Add("Novakov-Group-Mapped-Disk");
            //attribute.Properties["oMSyntax"].Add(64);
            //attribute.Properties["lDAPDisplayName"].Add("mappedDisk");
            //attribute.Properties["name"].Add("Novakov-Group-Mapped-Disk");
           
            //attribute.CommitChanges();

            Console.WriteLine("Creating attribute");

            DirectoryContext ctx = new DirectoryContext(DirectoryContextType.Forest);
            ActiveDirectorySchemaProperty property = new ActiveDirectorySchemaProperty(ctx, "mappedDisk");
            property.CommonName = "Novakov-Group-Mapped-Disk";
            property.IsSingleValued = false;
            property.Oid = "1.2.840.113556.1.8000.2554.45983.5507.44833.19347.44343.13141437.2987128.1";
            property.Syntax = ActiveDirectorySyntax.CaseIgnoreString;
            property.Save();

            Console.WriteLine("Attribute created");

            //ActiveDirectorySchemaProperty property = schema.FindProperty("mappedDisk");

            Console.WriteLine("Addiding attribute to class");
            ActiveDirectorySchemaClass groupClass = schema.FindClass("group");
            groupClass.OptionalProperties.Add(property);
            groupClass.Save();
        }
    }
}
